/*
 * Copyright 2008 CSC Consulting Group under the Apache License, 
 * Version 2.0 (the "License"); you may not use this file except in 
 * compliance with the License. You may obtain a copy of the License at 
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable 
 * law or agreed to in writing, software distributed under the License is 
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 * either express or implied. See the License for the specific language 
 * governing permissions and limitations under the License.
 */
package com.waw.journal.plugin;

import com.jivesoftware.base.User;
import com.jivesoftware.community.JiveGlobals;
import com.jivesoftware.community.ProfileManager;
import com.jivesoftware.community.UserProfile;
import com.jivesoftware.community.event.ProfileEvent;
import com.jivesoftware.community.event.ProfileListener;

/**
 * This class is registered as a listener to the ProfileEventDispatcher which notifies the 
 * class when a user profile has been modified.
 * The class reads the Profile Fields associated with the Fitness Journal and sets 
 * the extended properties of the User.
 * <p>
 * The fields to read from the profile are identified in the Journal administration page.
 * 
 * @see JournalConsts
 * .
 * @author Jim Tremlett
 */
public class UserProfileListener implements ProfileListener 
{
	/**
	 * This method is called when a user's profile has been updated.  This method copies the following profile fields to extended properties
	 * of the User object.
	 * <ul>
	 * <li>Fitness Journal User Name
	 * <li>Fitness Journal Password
	 * <li>Fitness Journal AutoLogin
	 * </ul>
	 * 
	 * @see JournalConsts
	 * @see com.jivesoftware.community.event.ProfileListener#profileUpdated(com.jivesoftware.community.event.ProfileEvent)
	 */
	public void profileUpdated(ProfileEvent evt) 
	{
		User user = evt.getUser();
		
		// get the IDs of the fitness journal profile fields
		String userNameFieldStr = JiveGlobals.getJiveProperty(JournalConsts.JOURNAL_USERNAME_FIELD);
		String passwordFieldStr = JiveGlobals.getJiveProperty(JournalConsts.JOURNAL_PASSWORD_FIELD);
		String autoLoginFieldStr = JiveGlobals.getJiveProperty(JournalConsts.JOURNAL_AUTOLOGIN_FIELD);
		
		// capture the user name field
		if(userNameFieldStr != null)
		{
			long userNameFieldID = Long.parseLong(userNameFieldStr);
			ProfileManager pManager = PluginUtils.getProfileManager();
			UserProfile userNamePF = pManager.getProfile(user).get(userNameFieldID);
			if(userNamePF != null) user.getProperties().put(JournalConsts.USER_JOURNAL_USERNAME, userNamePF.getValue());
		}
		
		// capture the user name field
		if(passwordFieldStr != null)
		{
			long passwordFieldID = Long.parseLong(passwordFieldStr);
			ProfileManager pManager = PluginUtils.getProfileManager();
			UserProfile passwordPF = pManager.getProfile(user).get(passwordFieldID);
			if(passwordPF != null) user.getProperties().put(JournalConsts.USER_JOURNAL_PASSWORD, passwordPF.getValue());
		}
		// capture the user name field
		if(autoLoginFieldStr != null)
		{
			long autoLoginFieldID = Long.parseLong(autoLoginFieldStr);
			ProfileManager pManager = PluginUtils.getProfileManager();
			UserProfile autoLoginPF = pManager.getProfile(user).get(autoLoginFieldID);
			if(autoLoginPF != null) user.getProperties().put(JournalConsts.USER_JOURNAL_AUTOLOGIN, autoLoginPF.getValue());
		}
	}
}
